home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / terminal / pr4w32 / pub / uudecode.c next >
Encoding:
C/C++ Source or Header  |  1995-12-04  |  2.6 KB  |  114 lines

  1. /* uudecode.c - convert ascii-encoded files back to their original form
  2.  * Usage: uudecode [infile]
  3.  *
  4.  * This command differs from the regular UNIX one in that the embedded
  5.  * file name "/dev/stdout" is recognized, allowing it to be used in a pipeline
  6.  *
  7.  * Written and placed in the public domain by Phil Karn, KA9Q 31 March 1987
  8.  */
  9. #include <stdio.h>
  10. #define    LINELEN    80
  11. #define MAXREC  64
  12.  
  13. main(argc,argv)
  14.  
  15. int argc;
  16. char *argv[];
  17.  
  18. {
  19.         char linebuf[LINELEN],*fgets();
  20.     register char *cp;
  21.     register int linelen,i;
  22.     FILE *in,*out;
  23.     
  24.         if(argc > 1)
  25.         {
  26.            if ((in = fopen(argv[1],"r")) == NULL)
  27.            {
  28.               fprintf(stderr,"Can't read %s\n",argv[1]);
  29.               exit(1);
  30.            }
  31.         }
  32.         else
  33.            in = stdin;
  34.  
  35.  
  36.         while(fgets(linebuf,LINELEN,in) != NULL)    // Find begin line
  37.         {
  38.            while ( linebuf[strlen(linebuf)-1] == '\n' )
  39.                 linebuf[strlen(linebuf)-1] = '\0';
  40.  
  41.            if(strncmp(linebuf,"begin",5) == 0)
  42.             break;
  43.         }
  44.  
  45.         if (feof(in))
  46.         {
  47.            fprintf(stderr,"No begin found\n");
  48.            exit(1);
  49.         }
  50.  
  51.     /* Find beginning of file name */
  52.         cp = &linebuf[6];
  53.  
  54.         while ( *cp && *cp == ' ')
  55.               cp++;
  56.  
  57.         while ( *cp && (*cp >= '0' && *cp <= '9' ))
  58.               cp++;
  59.  
  60.         while ( *cp && *cp == ' ')
  61.               cp++;
  62.  
  63.  
  64.  
  65.  
  66. //        if((cp = index(cp,' ')) != NULL)
  67. //                cp++;
  68.  
  69.     /* Set up output stream */
  70.         if(cp == NULL || strcmp(cp,"/dev/stdout") == 0)
  71.         {
  72.            out = stdout;
  73.         }
  74.         else if((out = fopen(cp,"wb")) == NULL)
  75.         {
  76.            fprintf(stderr,"Can't open %s\n",cp);
  77.            exit(1);
  78.         }
  79.  
  80.     /* Now crunch the input file */
  81.         while(fgets(linebuf,LINELEN,in) != NULL)
  82.         {
  83.  
  84.                 while ( linebuf[strlen(linebuf)-1] == '\n' )
  85.                      linebuf[strlen(linebuf)-1] = '\0';
  86.  
  87.  
  88.         linelen = linebuf[0] - ' ';
  89.         /* Some uuencodes put ` (64) instead of space (0) in the
  90.          * end-of-file record
  91.          */
  92.         linelen &= 63;
  93.         if(linelen == 0 || strncmp(linebuf,"end",3) == 0)
  94.                         break;
  95.  
  96.                 for(cp = &linebuf[1];linelen > 0;cp += 4)
  97.                 {
  98.                         for (i=0;i<4;i++)
  99.                         {
  100.                            cp[i] -= ' ';
  101.                            cp[i] &= 63;
  102.             }
  103.             putc((cp[0] << 2) | ((cp[1] >> 4) & 0x3),out);
  104.             if(--linelen > 0)
  105.                 putc((cp[1] << 4) | ((cp[2] >> 2) & 0xf),out);
  106.             if(--linelen > 0)
  107.                 putc((cp[2] << 6) | cp[3],out);
  108.             linelen--;
  109.         }
  110.         }
  111.         fclose(in);
  112.     fclose(out);
  113. }
  114.